package org.yoki.edu.image.frame;

import java.awt.*;
import java.awt.event.*;

/**
 * @author Sky$
 * @Description: TODO
 * @date 2018/2/9$ 10:42$
 */
public class MainWindow extends Frame {

    private static final String TITLE = "图像处理";

    /**
     * 面板
     */
    Panel panel;
    Button btnMedianFilter;
    Button btnOpenCvFilter;
    /**
     * 推出按钮
     */
    Button btnQuit;

    public MainWindow() {
        this.setTitle(TITLE);

        addWindowListener(new WindowAdapter() {

            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });

        btnMedianFilter = new Button("MedianFrame");
        btnOpenCvFilter = new Button("OpenCvFrame");
        btnQuit = new Button("Quit");

        panel = new Panel();
        panel.setBackground(Color.lightGray);
        this.add(panel, BorderLayout.SOUTH);
        //添加按钮
        panel.add(btnMedianFilter);
        panel.add(btnOpenCvFilter);
        panel.add(btnQuit);


        //按钮的动作程序  装载图像
        btnMedianFilter.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                medianFilterBtnActionPerformed(e);
            }
        });

        btnOpenCvFilter.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                openCvFilterBtnActionPerformed(e);
            }
        });

        //按钮的动作程序  退出
        btnQuit.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                System.exit(0);
            }
        });

    }

    public void medianFilterBtnActionPerformed(ActionEvent e) {
        MedianFrame ti = new MedianFrame();
        ti.setSize(800, 600);
        addOpenAndCloseListener(ti);
    }

    public void openCvFilterBtnActionPerformed(ActionEvent e) {
        OpenCvFrame ti = new OpenCvFrame();
        ti.setSize(800, 600);
        addOpenAndCloseListener(ti);
    }

    private void addOpenAndCloseListener(Window ti) {
        ti.addWindowListener(new WindowAdapter() {
            @Override
            public void windowOpened(WindowEvent e) {
                e.getWindow().setVisible(true);
                setVisible(false);
            }

            @Override
            public void windowClosing(WindowEvent e) {
                e.getWindow().setVisible(false);
                setVisible(true);
            }
        });
        ti.addComponentListener(new ComponentAdapter() {

            @Override
            public void componentShown(ComponentEvent e) {
                e.getComponent().setVisible(true);
                setVisible(false);
            }

            @Override
            public void componentHidden(ComponentEvent e) {
                e.getComponent().setVisible(false);
                setVisible(true);
            }
        });
        ti.setVisible(true);
    }

}
